local shenjian = fk.CreateSkill{
  name = "wk_heg__shenjian"
}

Fk:loadTranslationTable{
  ["wk_heg__shenjian"] = "申鉴",
  [":wk_heg__shenjian"] = "出牌阶段限一次，你可以与一名其他角色拼点，若你赢，其检索并获得一张普通锦囊牌，然后其可使用之且你可为此牌增加或减少任意个目标。",

  ["#wk_heg__shenjian"] = "申鉴：你可以与一名其他角色拼点",
  ["#wk_heg__shenjian-choose"] = "申鉴：你可以为 %arg 增加或减少任意个目标",
}

shenjian:addEffect("active",{
  anim_type = "support",
  prompt = "#wk_heg__shenjian",
  can_use = function (self, player)
    return player:usedSkillTimes(shenjian.name) < 1 and not player:isKongcheng()
  end,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected < 1 and player:canPindian(to_select)
  end,
  card_filter = Util.FalseFunc,
  min_target_num = 1,
  max_target_num = 1,
  on_use = function (self, room, skillUseEvent)
    local player = skillUseEvent.from
    local target = skillUseEvent.tos[1]
    local result = player:pindian({target}, shenjian.name)
    if result.results[target].winner ~= player then return end
    if player.dead or target.dead or #room:getCardsFromPileByRule(".|.|.|.|.|trick", 1, "allPiles") == 0 then return end
    local cards = {}
    local card
    while true do
      local id = room:getNCards(1)[1]
      room:turnOverCardsFromDrawPile(player, {id}, shenjian.name)
      room:delay(300)
      local c = Fk:getCardById(id)
      if c:isCommonTrick() then
        card = c
        break
      else
        table.insert(cards, id)
      end
    end
    room:moveCardTo(card, Player.Hand, target, fk.ReasonPrey, shenjian.name, nil, true, player, {"_wk_heg__shenjian", 1})
    if target.dead then return end
    room:askToUseRealCard(target,{
      pattern = ".|.|.|.|.|.|"..card.id,
      skill_name = shenjian.name,
    })
    room:setCardMark(card, "_wk_heg__shenjian", 0)
    room:cleanProcessingArea(cards)
  end
})

shenjian:addEffect(fk.TargetSpecifying, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(shenjian.name) and data.card:getMark("_wk_heg__shenjian") > 0 and data.firstTarget then
      local targets = data:getExtraTargets()
      local origin_targets = data.use.tos
      if #origin_targets > 1 then
        table.insertTable(targets, origin_targets)
      end
      if #targets > 0 then
        event:setCostData(self, {extra_data = targets})
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = #room.alive_players,
      targets = event:getCostData(self).extra_data,
      skill_name = shenjian.name,
      prompt = "#wk_heg__shenjian-choose:::"..data.card:toLogString(),
      cancelable = true,
      extra_data = table.map(data.use.tos, Util.IdMapper),
      target_tip_name = "addandcanceltarget_tip",
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local tos = event:getCostData(self).tos
    for _,to in ipairs(tos) do
      if table.contains(data.use.tos, to) then
        data:cancelTarget(to)
      else
        data:addTarget(to)
      end
    end
  end,
})

return shenjian